﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace publish_test
{
    /// <summary>
    ///  图片防盗链HttpHandler
    /// </summary>
    public class PictureHttpHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            //站点的域名
            string myDomain = "localhost";
            var s = context.Request.UrlReferrer;
            var host = context.Request.UrlReferrer.Host;
            if (context.Request.UrlReferrer == null ||
                context.Request.UrlReferrer.Host.ToLower().IndexOf(myDomain) < 0)
            {
                //如果是通过浏览器直接访问或者是通过其他站点访问过来的，则显示“资源不存在”图片
                //  context.Response.ContentType = "image/JPEG";
                // context.Response.WriteFile(context.Request.PhysicalApplicationPath + "/images/noimg.jpg");
                context.Response.ContentType = "text/html";
                context.Response.Write("资源不存在");
            }
            else
            {
                //如果是通过站内访问的，这正常显示图片
                context.Response.ContentType = "image/JPEG";
               // context.Response.WriteFile(context.Request.PhysicalPath);
                context.Response.WriteFile("3100.jpg");
            }
        }
    }
}